<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>扫一扫</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="referrer" content="never" />
		<link rel="stylesheet" href="../../css/mui.min.css">
		<link rel="stylesheet" href="static/css/common.css" />	
		
	</head>
	<body>
		<header class="mui-bar mui-bar-nav mui-bar-nav-bg">
			<h1 id="title" class="mui-title">扫一扫</h1>
		</header>
		<div id="content" style="margin-top: 60px;">
			<div class="flex-between"> 
				<button class="submit_btn" @click="goDetail" id="re-btn">开启扫码</button>
				<button class="cancel_btn" id="close-btn">关闭扫码</button>
			</div>
			
		</div>
		
		<script src="../../js/mui.min.js"></script>
		<script src="../../js/vue.js"></script>
		<script type="text/javascript" src="../../js/jquery-3.2.1.min.js"></script>
		<script type="text/javascript" src="static/js/neiosRequst.js"></script>
		<script type="text/javascript" src="static/js/neiosCommon.js"></script>
		<script type="text/javascript" charset="utf-8">
		scan = null; //扫描对象
			projectVM = new Vue({
				el: "#content",
				data: {
					keyManageId:'',
					user:''
				},
				mounted: function() {
					this.user = JSON.parse(localStorage.getItem('neiosUser'))
				},
				destroyed() {
						if(scan){
							scan.close()
						}
				},
				methods: {
					goDetail(){
						mui.openWindow({
								url: 'scanDetail.html',
								id: 'scanDetail.html',
						});
					},
					//扫码添加钥匙信息
					confrimKeyinfo(){
						var url = '/app/currentEntry/keyOperate/getKeyInfo'
						var that = this
						let param = {
							keyManageId:this.keyManageId,
						}
						mui.neiosRequst.httpRequst(url, param,
							function(data) {
								console.log(JSON.stringify(data))
							that.getKeyInfo(data.data.keyCode)
							},
							function() {
								console.log("查询失败!");
							}
						);
					},
				
				//获取钥匙信息
				getKeyInfo(e){
						var btnArray = ['取消', '确定'];
					var param = {
							keyCode:e,
							newStationCode:this.user.stationNewCode
						}
						var that = this
						mui.neiosRequst.httpRequst('/app/currentEntry/keyOperate/getBorrowInfoByCode', param,
						function(data) {
							console.log(JSON.stringify(data))
							if(data.data.length == 0){
							  mui.confirm('是否前往借用钥匙', '提示', btnArray, function(e) {
							  	if(e.index == 1) { 
									localStorage.setItem('scanKeyid',that.keyManageId)
							  		mui.openWindow({
							  	        url: 'dailyRecord/keyBorrow/borrow.html',
							  	        id: 'dailyRecord/keyBorrow/borrow.html',
							  			extras: {
							  					},
							  			waiting: {
							  			autoShow: false
							  					}
									});
										scan.close();  
										
							  	} else {
							  		scan.close()
							  		startRecognize()
							  	}
							  })
							}
							else{
								mui.neiosRequst.httpRequst('/app/currentEntry/keyOperate/getRepayInfoByCode', param,
								function(data) {
									console.log(JSON.stringify(data))
									if(data.data.length != 0){
										mui.confirm('是否前往归还钥匙', '提示', btnArray, function(e) {
											if(e.index == 1) { 
										localStorage.setItem('scanKeyid',that.keyManageId)
												mui.openWindow({
											        url: 'dailyRecord/keyBorrow/scanReturn.html',
											        id: 'dailyRecord/keyBorrow/scanReturn.html',
													extras: {
															},
													waiting: {
													autoShow: false
															}
														});
														
														scan.close();  
														startRecognize()
											} else {
												scan.close()
												startRecognize()
											}
										})
										
									}
									})
							
							}
						},
						function() {
							console.log("查询失败!");
						}
					);
				},
				
				},
				
				
			})
			
			$('#re-btn').on("click",function(e){	
				
			})
			$('#close-btn').on("click",function(e){	
				
				
			})
			console.log('扫码=================')
			
			mui.plusReady(function() {
				mui.init();
			});
			
			function startRecognize() {
				try {
					var filter;
					//自定义的扫描控件样式
					var styles = {
						top: '100px',
						left: '0px',
						width: '100%',
						height: '500px',
						position: 'static',
					}
					//扫描控件构造
					scan = plus.barcode.create('bcid', filter, styles);
					scan.onmarked = onmarked;
					scan.onerror = onerror;
					plus.webview.currentWebview().append(scan);
					scan.start();
			
				} catch (e) {
					alert("出现错误啦:\n" + e);
				}
			};
			 
			function onerror(e) {
				alert(e);
			};
			 
			function onmarked(type, result) {
				var text = '';
				switch (type) {
					case plus.barcode.QR:
						text = 'QR: ';
						break;
					case plus.barcode.EAN13:
						text = 'EAN13: ';
						break;
					case plus.barcode.EAN8:
						text = 'EAN8: ';
						break;
				}
				//扫描成功之后的处理
				mui.toast('扫描成功')
				console.log(JSON.stringify(result))
				if(result.split(',')[1] == 0){
					projectVM.keyManageId = result.split(',')[0]
					projectVM.confrimKeyinfo()
				}
				else if(result.split(',')[1] == 1){
					var that = this		
							var btnArray = ['取消', '确定'];
							mui.confirm('是否前往参与培训', '提示', btnArray, function(e) {
								if(e.index == 1) {
									localStorage.setItem('scanTrainid',result.split(',')[0])
									mui.openWindow({
								        url: 'education/train.html',
								        id: 'education/train.html',
										extras: {
	
												},
										waiting: {
										autoShow: false
												}
								});
								scan.close()
								javascript:window.history.back(-1);
								} else {
									scan.close()
									javascript:window.history.back(-1);
									startRecognize()
									
								}
							})
				}
				else{
					scan.close()
					
					mui.toast('请扫描正确二维码')
					setTimeout(function() {
						
						startRecognize()
					}, 2000);
					
				}

			};
			 
			
		</script>
	</body>
</html>
